<%@ page pageEncoding="UTF-8" import="com.mmp.main.entity.*, java.util.*, com.mmp.main.util.*"%>
<%@ page import="org.springframework.beans.factory.annotation.Autowired"%>
<%@ page import="org.springframework.web.context.support.SpringBeanAutowiringSupport"%>
<%!
    public void jspInit(){
        SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, getServletContext());
    }

	@Autowired
	protected com.mmp.main.DB.DBQueryManager dbQueryManager;
%>

<%
  Site currentSite = WebUtil.getSessionUser(request).getSite();
  Hospital currentLocation = WebUtil.getSessionLocation(request);
  if (currentSite.getContainsSeveralLocations() && request.getParameter("report") != null) {
 %>

<table class="layout_grid">
  <tr>
    <td class="side_label_top">نوع التقرير :</td>
    <td valign="top">  
    <table id="report_type_options_layout">
	  <tr>
	    <td colspan="3"><input name="reportType" type="radio" value="<%=Enums.REQUESTED_REPORT_TYPE.FOR_ALL_SITE_LOCATIONS.getId()%>">
	    مجمع</td>
	  </tr>
	  <%if(currentLocation != null){%>
	  <tr>
	    <td colspan="3"><input name="reportType" type="radio" value="<%=Enums.REQUESTED_REPORT_TYPE.FOR_CURRENT_LOCATION_ONLY.getId()%>">
	     للمستشفي أو المركز الصحي الحالي</td>
	  </tr>
	  <%}%>
	  <tr>
	    <td><input name="reportType" type="radio"  value="<%=Enums.REQUESTED_REPORT_TYPE.FOR_ANOTHER_LOCATION.getId()%>">
	    لمستشفي أو مركز صحي <%=(currentLocation != null)?" آخر":" منفرد" %></td>
	    <td>&nbsp;&nbsp;&nbsp;</td>
	    <td>
		    <select class="auto_off" name="reportLocation" id="reportLocation" onChange="setLocationCookie()">
		          <%
		          List<Hospital> locationList = dbQueryManager.getSiteLocations(currentSite.getId());
		      
		          if (locationList != null){
		            for (Hospital obj : locationList){
		           %>
		          <option value="<%=obj.getId()%>"><%=obj.getName()%></option>
		          <%}
		          }%>
		    </select>      
	    </td>
	  </tr>
  </table>
  </td>
  </tr>
</table>

<script>

  function setReportTypeCookie (){
  	$.removeCookie('reportType');
	$("#reportLocation").css('visibility', 'hidden');

	var reportType = $("input:radio[name='reportType']:checked").val();
	$.cookie('reportType', reportType);
	
	if (reportType == <%=Enums.REQUESTED_REPORT_TYPE.FOR_ANOTHER_LOCATION.getId()%>)
		$("#reportLocation").css('visibility', '');
	
  }
  
   function setLocationCookie(){
	    $.removeCookie('reportLocation');
		$.cookie('reportLocation', $('#reportLocation').val());
   }

   $(function(){
	   $("#reportLocation").css('visibility', 'hidden');
	   
	   // remove the current location from list
	
	   <%if(currentLocation != null){%>
	       $("#reportLocation option[value='<%=currentLocation.getId()%>']").remove();
	   <%}%>
	  
	   
	   $("input[name=reportType]").change(function(){setReportTypeCookie()});
	   
	   if($.cookie('reportType'))
	      setRadioCheckedValue("reportType",$.cookie('reportType'));
	   else
		  setRadioCheckedValue("reportType",'<%=Enums.REQUESTED_REPORT_TYPE.FOR_ALL_SITE_LOCATIONS.getId()%>'); 
	   
	   $("input:radio[name='reportType']:checked").change();
	   
	   if($.cookie('reportLocation')){
		  setComboSelectedValue("reportLocation", $.cookie('reportLocation'));
	      $("#reportLocation").change();
	   }
   })
  
</script>
<%} %>
